<?php
// +----------------------------------------------------------------------
// | Qbt CMS Plugin
// +----------------------------------------------------------------------
// | Copyright (c) 2012-2018 http://www.qbt8.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 黄老邪 <develop02@qbt8.com>
// +----------------------------------------------------------------------
namespace plugins\qbt_member_for_mall\controller;

use cmf\controller\PluginAdminBaseController;
use think\Db;

class AdminIndexController extends PluginAdminBaseController
{

    public function _initialize()
    {
        $adminId = cmf_get_current_admin_id();//获取后台管理员id，可判断是否登录
        if (!empty($adminId)) {
            $this->assign("admin_id", $adminId);
        } else {
            $this->error('未登录');
        }
    }

    public function index()
    {
        $list =  Db::table('qbtcmf_mall_members')->where("state != -2")->select();
        $this->assign("list", $list);
        return $this->fetch('/admin_index');
    }



    //添加
    public function add(){
        if ($_POST){
            $user_name = trim($this->request->param('user_name'));
            $info =  Db::table('qbtcmf_mall_members')->where("user_name = '{$user_name}' and state != -2")->find();
            if ($info){
                $this->error('该用户名已存在');
            }
            if ($this->request->param('a1')=='北京市' || $this->request->param('a1')=='天津市'){
                $data['area'] = $this->request->param('a2');
                $data['city'] = $this->request->param('a1');
//                $data['province'] = $this->request->param('a1');
            }elseif (empty($_POST['a3']) && !in_array($this->request->param('a1'),['北京市','天津市'])){
//                $data['area'] = $this->request->param('a2');
                $data['city'] = $this->request->param('a2');
                $data['province'] = $this->request->param('a1');
            }else{
                $data['area'] = $this->request->param('a3');
                $data['city'] = $this->request->param('a2');
                $data['province'] = $this->request->param('a1');
            }
            $data['sex'] = trim($this->request->param('sex'));
            $data['addr'] = trim($this->request->param('addr'));
            $data['mobile_phone'] = trim($this->request->param('mobile_phone'));
            $data['user_name'] = trim($this->request->param('user_name'));
            $data['update_time'] = time();
            $data['create_time'] = time();
            $result = Db::table('qbtcmf_mall_members')->insert($data);
            if ($result){
                $this->success('添加成功', cmf_plugin_url('QbtMemberForMall://AdminIndex/index'));
            }else{
                $this->error('添加失败');
            }
        }else{
            $lists = Db::table('qbtcmf_region')->where("level = 1")->select();
            $this->assign("list", $lists);
            return $this->fetch('/add');
        }
    }

    public function selajax(){
        $name = $this->request->param('name');
        $info =  Db::table('qbtcmf_region')->where("name = '{$name}'")->find();
        $list =  Db::table('qbtcmf_region')->where("pid = {$info['id']}")->select();
        return json_encode($list);
    }

    //编辑会员
    public function edit(){
        if ($_POST){
            $user_name = trim($this->request->param('user_name'));
            $user_id = trim($this->request->param('user_id'));
            $info =  Db::table('qbtcmf_mall_members')->where("user_name = '{$user_name}' and user_id != {$user_id}")->find();
            if ($info){
                $this->error('该用户名已存在');
            }
            if ($this->request->param('a1')=='北京市' || $this->request->param('a1')=='天津市'){
                $data['province'] = null;
                $data['area'] = $this->request->param('a2');
                $data['city'] = $this->request->param('a1');
//                $data['province'] = $this->request->param('a1');
            }elseif (empty($_POST['a3']) && !in_array($this->request->param('a1'),['北京市','天津市'])){
//                $data['area'] = $this->request->param('a2');
                $data['city'] = $this->request->param('a2');
                $data['province'] = $this->request->param('a1');
                $data['area'] = null;
            }else{
                $data['area'] = $this->request->param('a3');
                $data['city'] = $this->request->param('a2');
                $data['province'] = $this->request->param('a1');
            }
            $data['sex'] = trim($this->request->param('sex'));
            $data['addr'] = trim($this->request->param('addr'));
            $data['mobile_phone'] = trim($this->request->param('mobile_phone'));
            $data['user_name'] = trim($this->request->param('user_name'));
            $data['update_time'] = time();
            $result = Db::table('qbtcmf_mall_members')->where("user_id = {$user_id}")->update($data);
            if ($result){
                $this->success('保存成功', cmf_plugin_url('QbtMemberForMall://AdminIndex/index'));
            }else{
                $this->error('保存失败');
            }
        }else{
            $id = $this->request->param('id');
            $info =  Db::table('qbtcmf_mall_members')->where("user_id = '{$id}'")->find();
            $lists = Db::table('qbtcmf_region')->where("level = 1")->select();
            if (!$info['province']){
                $info1 = Db::table('qbtcmf_region')->where("name = '{$info['city']}'")->find();
                $lists1 = Db::table('qbtcmf_region')->where("pid = {$info1['id']}")->select();
            }elseif (!$info['area']) {
                $info1 =  Db::table('qbtcmf_region')->where("name = '{$info['province']}'")->find();
                $lists1 =  Db::table('qbtcmf_region')->where("pid = {$info1['id']}")->select();
            }else{
                $info1 =  Db::table('qbtcmf_region')->where("name = '{$info['province']}'")->find();
                $lists1 =  Db::table('qbtcmf_region')->where("pid = {$info1['id']}")->select();
                $info2 = Db::table('qbtcmf_region')->where("name = '{$info['city']}'")->find();
                $lists2 = Db::table('qbtcmf_region')->where("pid = {$info2['id']}")->select();
                $this->assign("list2", $lists2);
            }
            $this->assign("list1", $lists1);
            $this->assign("list", $lists);
            $this->assign("info", $info);
            return $this->fetch('/edit');
        }
    }

    //调节账户
    public function editzh(){
        if ($_POST){
            $data['user_id'] = $this->request->param('user_id');
            $data['remark'] = $this->request->param('remark')?:'';
            $data['type'] = $this->request->param('type');
            $data['num'] = $data['type']==1?$this->request->param('num'):'-'.$this->request->param('num');
            $data['operation_type'] = '后台操作';
            $data['create_time'] = time();
            $data['genre'] = $this->request->param('genre');
            $integral = $this->request->param('integral');
            $user_money = $this->request->param('user_money');
            $frozen_money = $this->request->param('frozen_money');
            switch ($data['genre'])
            {
                case 1:
                    $jf = $integral+$data['num'];
                    if ($jf<0){
                        $this->error('保存失败,扣除积分不得大于当前积分！');
                    }
                    $result = Db::table('qbtcmf_mall_members')->where("user_id = {$data['user_id']}")->update(['integral'=>$jf]);
                    break;
                case 2:
                    $yue = $user_money+$data['num'];
                    if ($yue<0 && $data['type']==1){
                        $this->error('保存失败,冻结金额不得大于可用余额！');
                    }
                    if ($yue<0 && $data['type']==2){
                        $this->error('保存失败,扣除冻结金额不得大于已冻结金额！');
                    }
                    $result = Db::table('qbtcmf_mall_members')->where("user_id = {$data['user_id']}")->update(['user_money'=>$yue]);
                    break;
                case 3:
                    $data1['user_money'] = $user_money-$data['num'];
                    $data1['frozen_money'] = $frozen_money+$data['num'];
                    if ($data['type']==1 && $data1['user_money']<0){
                        $this->error('保存失败,冻结金额不得大于可用余额！');
                    }elseif ($data['type']==2 && $data1['user_money']<0){
                        $this->error('保存失败,冻结金额不得大于可用余额！');
                    }
                    $result = Db::table('qbtcmf_mall_members')->where("user_id = {$data['user_id']}")->update($data1);
                    break;
            }
            if ($result){
                Db::table('qbtcmf_integral_log')->insert($data);
                $this->success('操作成功', cmf_plugin_url('QbtMemberForMall://AdminIndex/index'));
            }else{
                $this->error('操作失败');
            }
        }else{
            $id = $this->request->param('id');
            $info =  Db::table('qbtcmf_mall_members')->where("user_id = '{$id}'")->find();
            $this->assign("info", $info);
            return $this->fetch('/editzh');
        }
    }

    //批量删除
    public function del(){
        $id = $_GET['id'];
        $id = implode(',',$id);
        $result = Db::table('qbtcmf_mall_members')->where("user_id in ($id)")->update(['state'=>-2]);
        if ($result){
            echo 1;
        }else{
            echo -1;
        }
    }

    //删除
    public function del1(){
        $id = $this->request->param('id');
        $result = Db::table('qbtcmf_mall_members')->where("user_id = $id")->update(['state'=>-2]);
        if ($result){
            $this->success('删除成功');
        }else{
            $this->error('删除成功');
        }
    }

    //禁用或启用
    public function ajaxstate(){
        $id = $_GET['id'];
        $id = implode(',',$id);
        $state = $this->request->param('state', 0, 'intval');
        $result = Db::table('qbtcmf_mall_members')->where("user_id in ($id)")->update(['state'=>$state]);
        if ($result){
            echo 1;
        }else{
            echo -1;
        }
    }
}
